package com.parfinex.sims.servlet; /**
 * Description:
 *
 * @date 2021-11-16 17:01
 * @author lijh
 * @version 1.0
 */

import com.parfinex.sims.dao.ClassDao;
import com.parfinex.sims.entity.ClassInfo;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

@WebServlet(name = "QueryClassServlet", value = "/QueryClassServlet")
public class QueryClassServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        /**1、获取要查询的数据*/
        request.setCharacterEncoding("utf8");
        String className = request.getParameter("queryClassName");
        System.out.println("获取要查询的数据的className值为：" + className);
        /**执行ClassDao层的SQL语句*/
        ClassDao classDao = new ClassDao();
        List<ClassInfo> classInfos = null;
        try {
            classInfos = classDao.selectClass(className);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        System.out.println("获取到的记录有：" + classInfos.size() + "条");
        System.out.println("获取到的内容是：");
        for (ClassInfo classInfo : classInfos){
            System.out.println(classInfo.getClassName());
        }
        System.out.println("成功执行了for each循环");
        /**将处理结果响应到浏览器*/
        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();
        writer.write(String.valueOf(classInfos));
        request.setAttribute("classInfos",classInfos);
        request.getRequestDispatcher("classmanagement.jsp").forward(request,response);
//        response.sendRedirect("classmanagement.jsp");
    }
}
